package de.hwrBerlin.dataMining.view.dialog;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.ColorDialog;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.Text;

import de.hwrBerlin.dataMining.constants.ConfigConstants;
import de.hwrBerlin.dataMining.constants.Images;
import de.hwrBerlin.dataMining.constants.UIConstants;
import de.hwrBerlin.dataMining.controller.Controller;
import de.hwrBerlin.dataMining.view.TableView;
import de.hwrBerlin.tools.logger.LogLevel;
import de.hwrBerlin.tools.logger.Logger;

public class ConfigurationDialog extends Dialog {

	private int _returnValue = SWT.CANCEL;
	private Controller _controller;
	private TableView _tableView;
	private Shell _shell;
	private Display _display;
    private TabFolder _tabFolder;
	
	public ConfigurationDialog(Shell parent, int style, Controller pController, TableView pTableView) {
		super(parent, style);
		_controller = pController;
		_tableView = pTableView;
	}

	public int open() {

		Shell parent = getParent();
		_display = parent.getDisplay();

		_shell = new Shell(parent, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL | SWT.CLOSE | SWT.RESIZE);
		_shell.setText(UIConstants.CONFIGURATION);
		_shell.setImages(Images.LOGO_ICONS);
		GridLayout layout = new GridLayout(1, true);
		_shell.setLayout(layout);

		GridData dataForRightSide = new GridData(SWT.FILL, SWT.TOP, true, false);
		dataForRightSide.horizontalSpan = 2;
		dataForRightSide.minimumWidth = 50;
		dataForRightSide.horizontalAlignment = GridData.FILL;
		dataForRightSide.grabExcessHorizontalSpace = true;
		
		GridData dataForFarRightSide = new GridData(SWT.FILL, SWT.TOP, true, false);
		dataForFarRightSide.horizontalSpan = 1;
		dataForFarRightSide.horizontalAlignment = GridData.FILL;
		dataForFarRightSide.grabExcessHorizontalSpace = true;

		GridData dataForLeftSide = new GridData(SWT.FILL, SWT.TOP, true, false);
		dataForLeftSide.minimumWidth = 225;
		

		Button setDefault = new Button(_shell, SWT.PUSH);
		setDefault.setText(UIConstants.RESET_CONFIGURATION_TO_DEFAULT);
		setDefault.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

        _tabFolder = new TabFolder(_shell, SWT.NONE);
        _tabFolder.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

        // General -----------------------------------------------------------------------------------------------------------------------------------
        TabItem generalConfigGroupItem = new TabItem(_tabFolder, SWT.NONE);
        generalConfigGroupItem.setText(UIConstants.GENERAL_GROUP);
        Composite generalConfigGroup = new Composite(_tabFolder, SWT.NONE);
        generalConfigGroup.setLayout(new GridLayout(3, false));
        generalConfigGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        generalConfigGroupItem.setControl(generalConfigGroup);
        
        Label csvSeperatorLabel = new Label(generalConfigGroup, SWT.NONE);
        csvSeperatorLabel.setText(UIConstants.CSV_SEPERATOR);
        csvSeperatorLabel.setLayoutData(dataForLeftSide);

        final Text csvSeperator = new Text(generalConfigGroup, SWT.BORDER);
        csvSeperator.setSize(800, 80);
        csvSeperator.setTextLimit(10);
        csvSeperator.setLayoutData(dataForRightSide);
        
        Label sureToExitLabel = new Label(generalConfigGroup, SWT.NONE);
        sureToExitLabel.setText(UIConstants.SURE_TO_EXIT_CHECKBOX);
        sureToExitLabel.setLayoutData(dataForLeftSide);
        
        final Button sureToExitCheckBox = new Button(generalConfigGroup, SWT.CHECK);
        sureToExitCheckBox.setLayoutData(dataForRightSide);
        sureToExitCheckBox.setSelection(ConfigConstants.SURE_TO_EXIT);
        
        Label logLevelLabel = new Label(generalConfigGroup, SWT.NONE);
        logLevelLabel.setText(UIConstants.LOG_LEVEL);
        logLevelLabel.setLayoutData(dataForLeftSide);

        final Text logLevel = new Text(generalConfigGroup, SWT.BORDER);
        logLevel.setSize(800, 80);
        logLevel.setTextLimit(1);
        logLevel.setText(""+ConfigConstants.LOGLEVEL);
        logLevel.setLayoutData(dataForRightSide);

        // Table -----------------------------------------------------------------------------------------------------------------------------------
        TabItem tableGroupItem = new TabItem(_tabFolder, SWT.NONE);
        tableGroupItem.setText(UIConstants.TABLE_GROUP);
        Composite tableGroup = new Composite(_tabFolder, SWT.NONE);
        tableGroup.setLayout(new GridLayout(3, false));
        tableGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        tableGroupItem.setControl(tableGroup);

		Label maxAttributeLabel = new Label(tableGroup, SWT.NONE);
		maxAttributeLabel.setText(UIConstants.MAX_ATTRIBUTE);
		maxAttributeLabel.setLayoutData(dataForLeftSide);

		final Text maxAttribute = new Text(tableGroup, SWT.BORDER);
		maxAttribute.setLayoutData(dataForRightSide);

		Label maxLinesLabel = new Label(tableGroup, SWT.NONE);
		maxLinesLabel.setText(UIConstants.MAX_DATA);
		maxLinesLabel.setLayoutData(dataForLeftSide);

		final Text maxLines = new Text(tableGroup, SWT.BORDER);
		maxLines.setLayoutData(dataForRightSide);

		Label targetAttributeColorLabel = new Label(tableGroup, SWT.NONE);
		targetAttributeColorLabel.setText(UIConstants.TARGET_ATTRIBUTE_COLOR);
		targetAttributeColorLabel.setLayoutData(dataForLeftSide);
		
		final Label targetAttributeColorDemo = new Label(tableGroup, SWT.READ_ONLY);
		targetAttributeColorDemo.setLayoutData(dataForRightSide);
		targetAttributeColorDemo.addMouseListener(newMouseListenerForColorDemo(targetAttributeColorDemo));

        // Tree -----------------------------------------------------------------------------------------------------------------------------------
        TabItem treeGroupItem = new TabItem(_tabFolder, SWT.NONE);
        treeGroupItem.setText(UIConstants.TREE_GROUP);
        Composite treeGroup = new Composite(_tabFolder, SWT.NONE);
        treeGroup.setLayout(new GridLayout(3, false));
        treeGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        treeGroupItem.setControl(treeGroup);
		
		Label backgroundColorTreeLabel = new Label(treeGroup, SWT.NONE);
		backgroundColorTreeLabel.setText(UIConstants.BACKGROUND_COLOR_TREE);
		backgroundColorTreeLabel.setLayoutData(dataForLeftSide);

		final Label backgroundColorTreeDemo = new Label(treeGroup, SWT.READ_ONLY);
		backgroundColorTreeDemo.setLayoutData(dataForRightSide);
		backgroundColorTreeDemo.addMouseListener(newMouseListenerForColorDemo(backgroundColorTreeDemo));

		Label backgroundColorNodeLabel = new Label(treeGroup, SWT.NONE);
		backgroundColorNodeLabel.setText(UIConstants.BACKGROUND_COLOR_NODE);
		backgroundColorNodeLabel.setLayoutData(dataForLeftSide);

		final Label backgroundColorNodeDemo = new Label(treeGroup, SWT.READ_ONLY);
		backgroundColorNodeDemo.setLayoutData(dataForRightSide);
		backgroundColorNodeDemo.addMouseListener(newMouseListenerForColorDemo(backgroundColorNodeDemo));

		Label foregroundColorNodeLabel = new Label(treeGroup, SWT.NONE);
		foregroundColorNodeLabel.setText(UIConstants.FORGROUND_COLOR_NODE);
		foregroundColorNodeLabel.setLayoutData(dataForLeftSide);

		final Label foregroundColorNodeDemo = new Label(treeGroup, SWT.READ_ONLY);
		foregroundColorNodeDemo.setLayoutData(dataForRightSide);
		foregroundColorNodeDemo.addMouseListener(newMouseListenerForColorDemo(foregroundColorNodeDemo));

		Label backgroundColorLeafLabel = new Label(treeGroup, SWT.NONE);
		backgroundColorLeafLabel.setText(UIConstants.BACKGROUND_COLOR_LEAF);
		backgroundColorLeafLabel.setLayoutData(dataForLeftSide);

		final Label backgroundColorLeafDemo = new Label(treeGroup, SWT.READ_ONLY);
		backgroundColorLeafDemo.setLayoutData(dataForRightSide);
		backgroundColorLeafDemo.addMouseListener(newMouseListenerForColorDemo(backgroundColorLeafDemo));
		
		Label foregroundColorLeafLabel = new Label(treeGroup, SWT.NONE);
		foregroundColorLeafLabel.setText(UIConstants.FORGROUND_COLOR_LEAF);
		foregroundColorLeafLabel.setLayoutData(dataForLeftSide);

		final Label foregroundColorLeafDemo = new Label(treeGroup, SWT.READ_ONLY);
		foregroundColorLeafDemo.setLayoutData(dataForRightSide);
		foregroundColorLeafDemo.addMouseListener(newMouseListenerForColorDemo(foregroundColorLeafDemo));

		Label backgroundColorSelectedLabel = new Label(treeGroup, SWT.NONE);
		backgroundColorSelectedLabel.setText(UIConstants.BACKGROUND_COLOR_SELECTED);
		backgroundColorSelectedLabel.setLayoutData(dataForLeftSide);

		final Label backgroundColorSelectedDemo = new Label(treeGroup, SWT.READ_ONLY);
		backgroundColorSelectedDemo.setLayoutData(dataForRightSide);
		backgroundColorSelectedDemo.addMouseListener(newMouseListenerForColorDemo(backgroundColorSelectedDemo));

		Label foregroundColorSelectedLabel = new Label(treeGroup, SWT.NONE);
		foregroundColorSelectedLabel.setText(UIConstants.FORGROUND_COLOR_SELECTED);
		foregroundColorSelectedLabel.setLayoutData(dataForLeftSide);

		final Label foregroundColorSelectedDemo = new Label(treeGroup, SWT.READ_ONLY);
		foregroundColorSelectedDemo.setLayoutData(dataForRightSide);
		foregroundColorSelectedDemo.addMouseListener(newMouseListenerForColorDemo(foregroundColorSelectedDemo));


        // optimal tree -----------------------------------------------------------------------------------------------------------------------------------
        TabItem optimalTreeGroupItem = new TabItem(_tabFolder, SWT.NONE);
        optimalTreeGroupItem.setText(UIConstants.AUTOMATIC_TREE_GROUP);
        Composite optimalTreeGroup = new Composite(_tabFolder, SWT.NONE);
        optimalTreeGroup.setLayout(new GridLayout(3, false));
        optimalTreeGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        optimalTreeGroupItem.setControl(optimalTreeGroup);

        Label maxDataInLeafLabel = new Label(optimalTreeGroup, SWT.NONE);
        maxDataInLeafLabel.setText(UIConstants.MAX_DATA_IN_LEAF);
        maxDataInLeafLabel.setLayoutData(dataForLeftSide);
        
        final Text maxDataInLeaf = new Text(optimalTreeGroup, SWT.BORDER);
        maxDataInLeaf.setLayoutData(dataForRightSide);
        
		Button acceptButton = new Button(_shell, SWT.PUSH);
		acceptButton.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		acceptButton.setText(UIConstants.ACCEPT);
		acceptButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent pE) {	
				ConfigConstants.CSV_SEPERATOR = csvSeperator.getText();
				ConfigConstants.MAX_ATTRIBUTES = Integer.parseInt(maxAttribute.getText());
				ConfigConstants.MAX_ROWS = Integer.parseInt(maxLines.getText());
				ConfigConstants.TARGET_ATTRIBUTE_COLUMN_COLOR = targetAttributeColorDemo.getBackground().getRGB();
				ConfigConstants.ELEMENT_COUNT_TO_TERMINATE = Integer.parseInt(maxDataInLeaf.getText());
				ConfigConstants.TREE_BACKGROUND_COLOR = backgroundColorTreeDemo.getBackground().getRGB();
				ConfigConstants.TREE_NODE_BACKGROUND_COLOR = backgroundColorNodeDemo.getBackground().getRGB();
				ConfigConstants.TREE_NODE_FOREGROUND_COLOR = foregroundColorNodeDemo.getBackground().getRGB();
				ConfigConstants.TREE_LEAF_BACKGROUND_COLOR = backgroundColorLeafDemo.getBackground().getRGB();
				ConfigConstants.TREE_LEAF_FOREGROUND_COLOR = foregroundColorLeafDemo.getBackground().getRGB();
				ConfigConstants.TREE_SELECTED_BACKGROUND_COLOR = backgroundColorSelectedDemo.getBackground().getRGB();
				ConfigConstants.TREE_SELECTED_FOREGROUND_COLOR = foregroundColorSelectedDemo.getBackground().getRGB();
				ConfigConstants.SURE_TO_EXIT = sureToExitCheckBox.getSelection();
				try {
	                ConfigConstants.LOGLEVEL = Integer.parseInt(logLevel.getText());
                }
                catch (NumberFormatException e) {
                    ConfigConstants.LOGLEVEL = 1;
                }
				switch (ConfigConstants.LOGLEVEL) {
		            case 1: Logger.setCurrentLogLevel(LogLevel.DEBUG);           
		            break;
		            case 2: Logger.setCurrentLogLevel(LogLevel.INFO);           
		            break;
		            case 3: Logger.setCurrentLogLevel(LogLevel.ERROR);           
		            break;
		            default: Logger.setCurrentLogLevel(LogLevel.DEBUG);
		            break;
		        }
				
				ConfigConstants.saveConstants();
				
				_controller.updateTrees();
				if(_controller.getModel().getTable() != null){
					_tableView.updateAllTable(_controller.getModel().getTable());
				}
			}
		});
		
		Button okButton = new Button(_shell, SWT.PUSH);
		okButton.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		okButton.setText(UIConstants.OK);
		okButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent pE) {

				ConfigConstants.CSV_SEPERATOR = csvSeperator.getText();
				ConfigConstants.MAX_ATTRIBUTES = Integer.parseInt(maxAttribute.getText());
				ConfigConstants.MAX_ROWS = Integer.parseInt(maxLines.getText());
				ConfigConstants.TARGET_ATTRIBUTE_COLUMN_COLOR = targetAttributeColorDemo.getBackground().getRGB();
				ConfigConstants.ELEMENT_COUNT_TO_TERMINATE = Integer.parseInt(maxDataInLeaf.getText());
				ConfigConstants.TREE_BACKGROUND_COLOR = backgroundColorTreeDemo.getBackground().getRGB();
				ConfigConstants.TREE_NODE_BACKGROUND_COLOR = backgroundColorNodeDemo.getBackground().getRGB();
				ConfigConstants.TREE_NODE_FOREGROUND_COLOR = foregroundColorNodeDemo.getBackground().getRGB();
				ConfigConstants.TREE_LEAF_BACKGROUND_COLOR = backgroundColorLeafDemo.getBackground().getRGB();
				ConfigConstants.TREE_LEAF_FOREGROUND_COLOR = foregroundColorLeafDemo.getBackground().getRGB();
				ConfigConstants.TREE_SELECTED_BACKGROUND_COLOR = backgroundColorSelectedDemo.getBackground().getRGB();
				ConfigConstants.TREE_SELECTED_FOREGROUND_COLOR = foregroundColorSelectedDemo.getBackground().getRGB();
				ConfigConstants.SURE_TO_EXIT = sureToExitCheckBox.getSelection();
                try {
                    ConfigConstants.LOGLEVEL = Integer.parseInt(logLevel.getText());
                }
                catch (NumberFormatException e) {
                    ConfigConstants.LOGLEVEL = 1;
                }
                switch (ConfigConstants.LOGLEVEL) {
                    case 1: Logger.setCurrentLogLevel(LogLevel.DEBUG);           
                    break;
                    case 2: Logger.setCurrentLogLevel(LogLevel.INFO);           
                    break;
                    case 3: Logger.setCurrentLogLevel(LogLevel.ERROR);           
                    break;
                    default: Logger.setCurrentLogLevel(LogLevel.DEBUG);
                    break;
                }

				ConfigConstants.saveConstants();
				_returnValue = SWT.OK;
				_shell.close();
			}
		});

		Button cancelButton = new Button(_shell, SWT.PUSH);
		cancelButton.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		cancelButton.setText(UIConstants.CANCEL);
		cancelButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent pE) {
				_shell.close();
			}
		});

		setDefault.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				// setzen der Werte
				csvSeperator.setText(ConfigConstants.DEFAULT_CSV_SEPERATOR);

				maxAttribute.setText("" + ConfigConstants.DEFAULT_MAX_ATTRIBUTES);
				maxLines.setText("" + ConfigConstants.DEFAULT_MAX_ROWS);
				targetAttributeColorDemo.setBackground(new Color(_display, ConfigConstants.DEFAULT_TARGET_ATTRIBUTE_COLUMN_COLOR));

				maxDataInLeaf.setText("" + ConfigConstants.DEFAULT_ELEMENT_COUNT_TO_TERMINATE);
				
				backgroundColorTreeDemo.setBackground(new Color(_display, ConfigConstants.DEFAULT_TREE_BACKGROUND_COLOR));
				backgroundColorNodeDemo.setBackground(new Color(_display, ConfigConstants.DEFAULT_TREE_NODE_BACKGROUND_COLOR));
				foregroundColorNodeDemo.setBackground(new Color(_display, ConfigConstants.DEFAULT_TREE_NODE_FOREGROUND_COLOR));
				backgroundColorLeafDemo.setBackground(new Color(_display, ConfigConstants.DEFAULT_TREE_LEAF_BACKGROUND_COLOR));
				foregroundColorLeafDemo.setBackground(new Color(_display, ConfigConstants.DEFAULT_TREE_LEAF_FOREGROUND_COLOR));
				backgroundColorSelectedDemo.setBackground(new Color(_display, ConfigConstants.DEFAULT_TREE_SELECTED_BACKGROUND_COLOR));
				foregroundColorSelectedDemo.setBackground(new Color(_display, ConfigConstants.DEFAULT_TREE_SELECTED_FOREGROUND_COLOR));
				
				sureToExitCheckBox.setSelection(ConfigConstants.DEFAULT_SURE_TO_EXIT);
		        logLevel.setText(""+ConfigConstants.DEFAULT_LOGLEVEL);
			}
		});

		// setzen der Werte
		csvSeperator.setText(ConfigConstants.CSV_SEPERATOR);

		maxAttribute.setText("" + ConfigConstants.MAX_ATTRIBUTES);
		maxLines.setText("" + ConfigConstants.MAX_ROWS);
		targetAttributeColorDemo.setBackground(new Color(_display, ConfigConstants.TARGET_ATTRIBUTE_COLUMN_COLOR));

		maxDataInLeaf.setText("" + ConfigConstants.ELEMENT_COUNT_TO_TERMINATE);
		backgroundColorTreeDemo.setBackground(new Color(_display, ConfigConstants.TREE_BACKGROUND_COLOR));
		backgroundColorNodeDemo.setBackground(new Color(_display, ConfigConstants.TREE_NODE_BACKGROUND_COLOR));
		foregroundColorNodeDemo.setBackground(new Color(_display, ConfigConstants.TREE_NODE_FOREGROUND_COLOR));
		backgroundColorLeafDemo.setBackground(new Color(_display, ConfigConstants.TREE_LEAF_BACKGROUND_COLOR));
		foregroundColorLeafDemo.setBackground(new Color(_display, ConfigConstants.TREE_LEAF_FOREGROUND_COLOR));
		backgroundColorSelectedDemo.setBackground(new Color(_display, ConfigConstants.TREE_SELECTED_BACKGROUND_COLOR));
		foregroundColorSelectedDemo.setBackground(new Color(_display, ConfigConstants.TREE_SELECTED_FOREGROUND_COLOR));
        
        sureToExitCheckBox.setSelection(ConfigConstants.SURE_TO_EXIT);
        logLevel.setText(""+ConfigConstants.LOGLEVEL);

		_shell.pack();
		_shell.open();
		while (!_shell.isDisposed()) {
			if (!_display.readAndDispatch()) {
				_display.sleep();
			}
		}

		return _returnValue;
	}

	private MouseListener newMouseListenerForColorDemo(final Label pColorDemo) {
		return new MouseListener(){
			@Override
			public void mouseDoubleClick(MouseEvent arg0) {
			}
			@Override
			public void mouseDown(MouseEvent arg0) {
			}
			public void mouseUp(MouseEvent arg0) {
				RGB rgb = new ColorDialog(_shell, SWT.NONE).open();
				if(rgb != null){
					pColorDemo.setBackground(new Color(_display, rgb));
				}				
			}
		};
	}

}
